﻿using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ChangeText
{
    internal static class Extensions
    {
        public static string SplitForConsole(this string s, List<char> charactersToSplitWith=null)
        {
            if (s.Length <= 80)
            {
                return s;
            }
            var output = new StringBuilder();
            var splitList = charactersToSplitWith ?? new List<char> {' ', '/', '\\', '-', '_'};
            do
            {
                if (s.Length <= 79)
                {
                    output.Append(s);
                    break;
                }
                var currentString = s.Substring(0, 79);
                var listOfIndexes = splitList.Select(c => currentString.LastIndexOf(c)).ToList();

                if (listOfIndexes.All(i => i <= 0))
                {
                    output.AppendLine(currentString);
                    s = s.Substring(79);
                    continue;
                }
                var indexOfCharToSplitCurrentString = listOfIndexes.Max();
                var currentLine = currentString.Substring(0, indexOfCharToSplitCurrentString + 1);
                output.AppendLine(currentLine);
                s = string.Concat("\t",s.Substring(indexOfCharToSplitCurrentString + 1));

            } while (s.Length > 0);
            return output.ToString();
        }
    }
}
